library example_package;

import 'package:example_package/arg/package1arg.dart';
import 'package:riki_router/riki_router.dart';

import 'listener/package_package2_listener.dart';

class ExamplePackage extends RikiBasePackage {
  // 工厂模式
  factory ExamplePackage() => _getInstance();

  static ExamplePackage get instance => _getInstance();
  static ExamplePackage? _instance;

  ExamplePackage._internal();

  static ExamplePackage _getInstance() {
    if (_instance == null) {
      _instance = ExamplePackage._internal();
    }
    return _instance!;
  }

  static const String PACKAGE = 'example_package';

  bool _isInit = false;

  void init() {
    if (_isInit) return;
    _isInit = true;

    RikiRouter.instance.expandConvert(<T extends Object?>(dynamic value) {
      if (Package1Arg.fromJson({}) is T && value is Map<dynamic, dynamic>) {
        return Package1Arg.fromJson(value) as T;
      }
      return null;
    });

    registerRikiEvent();
  }

  void reset() {
    unregisterRikiEvent();
  }

  @override
  void onRikiEvent(RikiEventType type, arguments,callback) {
    print('example_package:$type,$arguments');
    if (type.value == 'PACKAGE1_2') {
      fire<Package2Listener>(arguments);
    }
  }

  @override
  String get packageName => 'package';

  @override
  List<String> get registerEvents => [];
}
